package com.shms.action;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shms.bean.GroupOfTrends;
import com.shms.constant.QueryConstant;
import com.shms.entity.ShTrends;
import com.shms.service.ShTrendsService;

/**
 * 文件名：TrendsAction.java
 * 编写者：陆遥
 * 创建时间：2013-1-3
 * 简要描述：
 * 动态信息的控制文件
 */
public class TrendsAction extends ActionSupport {
	//序列化
	private static final long serialVersionUID = -7106008904232245508L;
	private ShTrendsService shTrendsService;
	private GroupOfTrends groupOfTrends;
	private ShTrends shTrends;
	private String message;
	private Integer[] nos;

	public String addTrends() {
		return SUCCESS;
	}
	
	/**
	 * 保存动态信息
	 * @return
	 */
	public String saveTrends() {
		boolean rtn = shTrendsService.saveTrends(shTrends);
		if (rtn == false) {
			message = "保存失败";
		} else {
			message = "保存成功";
		}
		return SUCCESS;
	}
	
	public String queryByTrendsId() {
		//获取要查询的单条记录id
		long id = Long.parseLong(ServletActionContext.getRequest().getParameter("no"));
		//参数封装
		ShTrends shTrendsParams = new ShTrends();
		shTrendsParams.setTid(id);
		//查询
		shTrends = shTrendsService.queryById(shTrendsParams);
		return SUCCESS;
	}
	
	/**
	 * 根据分页信息，查询动态信息列表
	 * @return 
	 */
	public String queryTrends() {
		//获取传来的当前页数
		Integer currentPage = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
		//获取传来的上一页或下一页标志
		String type = ServletActionContext.getRequest().getParameter("param");
		//查询动态信息的分页信息的输入参数的封装
		GroupOfTrends groupOfTrendsParams = new GroupOfTrends();
		/*
		 * 将对应的输入参数：
		 * 当前页数，上一页下一页标志，每页显示条数
		 * 分别赋值到类的对应字段中
		 */
		groupOfTrendsParams.setCurrentPage(currentPage);
		groupOfTrendsParams.setType(type);
		groupOfTrendsParams.setPageSize(QueryConstant.F_Pagesize);
		//查询
		groupOfTrends = shTrendsService.queryTrends(groupOfTrendsParams);
		if (groupOfTrends.getShTrends() == null)
			return INPUT;
		else 
			return SUCCESS;
	}
	/**
	 * 批量删除动态信息
	 * @return 
	 */
	public String deleteTrends() {
		//获取要删除的单条记录id
		if (ServletActionContext.getRequest().getParameter("no") != null ) {
			Integer id = Integer.parseInt(ServletActionContext.getRequest().getParameter("no"));
			Integer[] ids = {id};
			nos = ids;
		}
		//删除成功重新查询数据，并返回
		if(shTrendsService.batchDelete(nos)){
			//获取传来的当前页数
			Integer currentPage = groupOfTrends.getCurrentPage() - 1;
			//获取传来的上一页或下一页标志
			String type = groupOfTrends.getType();
			//构造查询动态信息的分页信息输入参数
			GroupOfTrends groupOfTrendsParams = new GroupOfTrends();
			groupOfTrendsParams.setCurrentPage(currentPage);
			groupOfTrendsParams.setType(type);
			groupOfTrendsParams.setPageSize(QueryConstant.F_Pagesize);
			//查询
			groupOfTrends = shTrendsService.queryTrends(groupOfTrendsParams);
			return SUCCESS;
		}else{
			message = "删除动态信息失败";
			return INPUT;
		}
	}

	public Integer[] getNos() {
		return nos;
	}

	public void setNos(Integer[] nos) {
		this.nos = nos;
	}

	public ShTrendsService getShTrendsService() {
		return shTrendsService;
	}
	
	/**
	 * Spring 依赖注入
	 * @param shTrendsService
	 */
	public void setShTrendsService(ShTrendsService shTrendsService) {
		this.shTrendsService = shTrendsService;
	}

	public GroupOfTrends getGroupOfTrends() {
		return groupOfTrends;
	}

	public void setGroupOfTrends(GroupOfTrends groupOfTrends) {
		this.groupOfTrends = groupOfTrends;
	}

	public ShTrends getShTrends() {
		return shTrends;
	}

	public void setShTrends(ShTrends shTrends) {
		this.shTrends = shTrends;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}	
}
